package com.rightsidetech.http.controller.admin.advert;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.rightsidetech.activity.entity.MtActivity;
import com.rightsidetech.activity.service.MtActivityService;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.enums.ModifyName;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.ModifyLog;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.mall.entity.MallCommodityWithBLOBs;
import com.rightsidetech.mall.service.MallCommodityService;
import com.rightsidetech.system.dtos.request.adverts.*;
import com.rightsidetech.system.dtos.response.adverts.BannerPageResp;
import com.rightsidetech.system.service.SysAdvertsBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.List;

@RestController
@RequestMapping("/admin/banner/")
@ShenyuSpringMvcClient(path = "/admin/banner/**")
@Api(tags = "banner相关")
public class AdvertsBannerController extends BaseController {

    @Resource
    private SysAdvertsBannerService sysAdvertsBannerService;
    @Resource
    private MallCommodityService mallCommodityService;
    @Resource
    private MtActivityService mtActivityService;

    @Token
    @Log
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/save")
    @ApiOperation("添加banner")
    @ModifyLog(serviceClass = SysAdvertsBannerService.class,modifyType = ModifyName.SAVE)
    public ResponseData save(@RequestBody BannerSaveReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(null == loginInfoId || null == loginName){
            return ResponseData.fail("未找到登录人");
        }
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        ResponseData<String> check = check(req.getBannerType(), req.getJumpId());
        if(check.getCodeId() != 1){
            return check;
        }
        return sysAdvertsBannerService.saveBanner(req, loginName,check.getResData());
    }

    @Token
    @Log
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("分页查询banner")
    public ResponseData<PageInfo<BannerPageResp>> page(BannerPageReq req){
         return sysAdvertsBannerService.pageBanner(req);
    }

    @Token
    @Log
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/list")
    @ApiOperation("不分页排除无效数据")
    @ApiOperationSupport(ignoreParameters = {"pageNo", "pageSize"})
    public ResponseData<List<BannerPageResp>> list(BannerPageReq req){
        return sysAdvertsBannerService.list(req);
    }

    @Token
    @Log
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/update")
    @ApiOperation("修改banner")
    @ModifyLog(serviceClass = SysAdvertsBannerService.class,entityClass = BannerPageResp.class,idType = List.class)
    public ResponseData upBanner(@RequestBody BannerUpReq req){
        ResponseData<String> check = check(req.getBannerType(), req.getJumpId());
        if(check.getCodeId() != 1){
            return check;
        }
        return sysAdvertsBannerService.update(req, check.getResData());
    }

    @Token
    @Log
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/del")
    @ApiOperation("删除banner")
    @ModifyLog(serviceClass = SysAdvertsBannerService.class,entityClass = BannerPageResp.class,idType = List.class)
    public ResponseData delBanner(@RequestBody DelReq req){
        return sysAdvertsBannerService.del(req);
    }

    @Token
    @Log
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/is_close")
    @ApiOperation("关闭打开banner")
    @ModifyLog(serviceClass = SysAdvertsBannerService.class,entityClass = BannerPageResp.class,idType = List.class)
    public ResponseData isClose(@RequestBody CloseLoadReq req){
        return sysAdvertsBannerService.isClose(req);
    }

    @Token
    @Log
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/sort")
    @ApiOperation("排序banner")
    @ModifyLog(serviceClass = SysAdvertsBannerService.class,entityClass = BannerPageResp.class,idType = List.class)
    public ResponseData sort(@RequestBody BannerSortReq req){
        return sysAdvertsBannerService.sortBanner(req);
    }




    private ResponseData<String> check(Byte bannerType,Long jumpId){
        //banner其他两种类型待开发
        if(bannerType == 2){
            if (jumpId != null) {
                MallCommodityWithBLOBs commodity = mallCommodityService.selectByPrimaryKey(jumpId);
                if(ObjectUtil.isEmpty(commodity)){
                    return ResponseData.fail("跳转的商品不存在");
                }
                if(commodity.getCommodityStatus() != 3){
                    return ResponseData.fail("此商品并不是上架状态不可跳转");
                }
                return ResponseData.success(commodity.getCommodityName());
            }
        }
        if(bannerType == 0){
            if(null != jumpId){
                //查找活动名称
                MtActivity mtActivity = mtActivityService.selectById(jumpId);
                if(!StringUtils.isEmpty(mtActivity)){
                    return ResponseData.success(mtActivity.getName());
                }
            }

        }
        return ResponseData.success();
    }
}
